﻿using System;
using Mekiasm.Common;
namespace Mekiasm.ElectricElement
{
    public class SolarPanelElement : Common.ElectricElement
    {
        public SolarPanelElement(int x, int y, int z) : base(x, y, z)
        {
            Sound = new SoundItem("solargenerator");

        }
        public override bool Simulate()
        {
            var Component = Instance.Machine;
            int light = ILibrary.SubsystemTerrain.Terrain.GetCellLightFast(Point.X, Point.Y, Point.Z);
            light = Math.Min(light, ILibrary.SubsystemSky.SkyLightValue);
            float time = ILibrary.subsystemTimeOfDay.TimeOfDay;
            if (time >= 0.25f && time <= 0.75f && Component.Power < Component.MaxPower)
            {
                int GetPower = (int)(10f * (light / 15f));
                if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(1.0, 0.0)) Component.InputPowerLimit = GetPower;
                if (Component.Power + GetPower <= Component.MaxPower)
                {
                    Component.Power += GetPower;
                }
                else Component.Power += (Component.MaxPower - Component.Power);
                Component.IsCrafting = true;
            }
            else
            {
                Component.IsCrafting = false;
            }
            PlaySound();
            return base.Simulate();
        }
    }
}
